﻿import com.dinmax.flash.central.extension.user.*
import com.dinmax.flash.utilities.xml.XMLUtils

class AddressFactory {

	public static function getAddresses(node:XMLNode):Array {
		var ads:Array = new Array();
		for(var ad:XMLNode = node.firstChild; ad != null; ad=ad.nextSibling){
			if(ad.nodeType == 1){
				ads.push(AddressFactory.getAddress(ad));
			};
		};
		return ads;
	};
	
	public static function getAddress(node:XMLNode):Address {
		var ad:Address = new Address();
		try {
			ad.setId(XMLUtils.getAttribute(node, "id"));
			ad.setName(XMLUtils.getAttribute(node, "name"));
			ad.setPhone(Number(XMLUtils.getAttribute(node, "phonenumber")));
			//ad.setZip(XMLUtils.getAttribute(node, "zip"));
			ad.setDepartment(AddressFactory.getDepartment(XMLUtils.getNode(node, "department")));
		} catch(e) {
			trace("ERROR EN USER FACTORY creando el ADDRESS "+e);
		};
		return ad;
	}; //fin funci
	
	public static function getDepartment(node:XMLNode):Department {
		var dp:Department = new Department();
		try {
			dp.setId(XMLUtils.getAttribute(node, "id"));
			dp.setName(XMLUtils.getAttribute(node, "name"));
			dp.setState(AddressFactory.getStateMap(XMLUtils.getNode(node, "state")));
		} catch(e) {
			trace("ERROR EN USER FACTORY creando el ADDRESS "+e);
		};
		return dp;
	}; //fin funci
	
	public static function getStateMap(node:XMLNode):StateMap {
		var st:StateMap = new StateMap();
		try {
			st.setId(XMLUtils.getAttribute(node, "id"));
			st.setName(XMLUtils.getAttribute(node, "name"));
			st.setCountry(AddressFactory.getCountry(XMLUtils.getNode(node, "country")));
		} catch(e) {
			trace("ERROR EN USER FACTORY creando el ADDRESS "+e);
		};
		return st;
	}; //fin funci
	
	public static function getCountry(node:XMLNode):Country {
		var ct:Country = new Country();
		try {
			ct.setId(XMLUtils.getAttribute(node, "id"));
			ct.setName(XMLUtils.getAttribute(node, "name"));
		} catch(e) {
			trace("ERROR EN USER FACTORY creando el ADDRESS "+e);
		};
		return ct;
	}; //fin funci	
	
}; // fin de clase